package com.wxy.xr.dao.impl;

import com.wxy.xr.bean.Company;
import com.wxy.xr.dao.CompanyDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.ArrayList;
import java.util.List;


public class CompanyDaoImpl extends BaseDaoImpl<Company> implements CompanyDao {


    @Override
    public boolean save(Company bean) {
        Integer id=bean.getId();
        List<Object> args = new ArrayList<>();
        args.add(bean.getName());
        args.add(bean.getLogo());
        args.add(bean.getWebsite());
        args.add(bean.getIntro());
        String sql;
        if(id==null || id<1){
            sql="INSERT INTO company(name,logo,website,intro) values(?,?,?,?)";
        } else {
            sql="UPDATE company set name=? ,logo=? ,website=? , intro=? where id =?";
            args.add(id);
        }
        return tpl.update(sql,args.toArray())>0;
    }

    @Override
    public Company get(Integer id) {
        String sql="select id, created_time, name,logo ,website, intro from company where id=?";
        return tpl.queryForObject(sql,new BeanPropertyRowMapper<>(Company.class),id);
    }

    @Override
    public List<Company> list() {
        String sql="select id, created_time, name,logo, website, intro from company" ;
        return tpl.query(sql,new BeanPropertyRowMapper<>(Company.class));
    }
}
